package com.adream.scalpel.app.client.ui;

import com.adream.scalpel.app.client.service.UserManagement;
import com.adream.scalpel.app.client.service.UserManagementAsync;
import com.adream.scalpel.common.client.vo.UserVO;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.TextBox;

import java.util.ArrayList;

public class SearchUsers extends Composite {

    private FlexTable searchUsersFlexTable;
    private Label editLabel;
    private Label enabledLabel;
    private Label label;
    private Grid searchResultGrid;
    private PushButton searchButton;
    private Label nameLabel;
    private TextBox userStringTextBox;
    private FlexTable flexTable;
    private ArrayList<UserVO> currentSearchResult;
    private UserEditDialogBox userEditDialogBox;

    public SearchUsers() {
        searchUsersFlexTable = new FlexTable();
        initWidget(searchUsersFlexTable);
        searchUsersFlexTable.setWidth("100%");

        flexTable = new FlexTable();

        searchUsersFlexTable.setWidget(0, 0, flexTable);
        flexTable.setWidth("100%");
        searchUsersFlexTable.getCellFormatter().setVerticalAlignment(0, 0,
                HasVerticalAlignment.ALIGN_TOP);
        searchUsersFlexTable.getCellFormatter().setHorizontalAlignment(0, 0,
                HasHorizontalAlignment.ALIGN_CENTER);
        searchUsersFlexTable.getCellFormatter().setWidth(0, 0, "100%");

        userStringTextBox = new TextBox();
        flexTable.setWidget(0, 1, userStringTextBox);
        flexTable.getCellFormatter().setWidth(0, 1, "50%");
        userStringTextBox.setText("");

        nameLabel = new Label("Name");
        flexTable.setWidget(0, 0, nameLabel);
        flexTable.getCellFormatter().setWidth(0, 0, "50%");
        flexTable.getCellFormatter().setHorizontalAlignment(0, 0,
                HasHorizontalAlignment.ALIGN_RIGHT);

        searchButton = new PushButton();
        flexTable.setWidget(1, 1, searchButton);
        searchButton.setWidth("70");
        searchButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                onClickSearchButton(event);
            }
        });
        searchButton.setText("Search");

        searchResultGrid = new Grid(1, 3);
        searchUsersFlexTable.setWidget(1, 0, searchResultGrid);
        searchResultGrid.setWidth("100%");
        searchUsersFlexTable.getCellFormatter().setVerticalAlignment(1, 0,
                HasVerticalAlignment.ALIGN_TOP);
        searchUsersFlexTable.getCellFormatter().setHorizontalAlignment(1, 0,
                HasHorizontalAlignment.ALIGN_CENTER);

        label = new Label("Name");
        searchResultGrid.setWidget(0, 0, label);
        searchResultGrid.getCellFormatter().setWidth(0, 0, "50%");

        enabledLabel = new Label("Enabled");
        searchResultGrid.setWidget(0, 1, enabledLabel);
        searchResultGrid.getCellFormatter().setWidth(0, 1, "25%");

        editLabel = new Label("Edit");
        searchResultGrid.setWidget(0, 2, editLabel);
        searchResultGrid.getCellFormatter().setWidth(0, 2, "25%");

    }

    public void addOrUpdateSearchResult(UserVO userToAddUpdate) {
        for (UserVO user : currentSearchResult) {
            if (user.getUserName().equals(userToAddUpdate.getUserName())) {
                currentSearchResult.remove(user);
                currentSearchResult.add(userToAddUpdate);
                break;
            }
        }
        onSearchResultChanged();
    }

    protected void onClickEditButton(String userName) {
        for (UserVO user : currentSearchResult) {
            if (userName.equals(user.getUserName())) {
                if (userEditDialogBox == null) {
                    userEditDialogBox = new UserEditDialogBox(this);
                }
                userEditDialogBox.setUserAndShow(user);
                break;
            }
        }
    }

    protected void onClickSearchButton(final ClickEvent event) {
        UserManagementAsync userManagementAsync = UserManagement.Util.getInstance();
        userManagementAsync.searchUserByDisplayName(userStringTextBox.getText(),
                new AsyncCallback<ArrayList<UserVO>>() {

                    public void onFailure(Throwable caught) {
                        Window.alert(caught.getMessage());
                    }

                    public void onSuccess(ArrayList<UserVO> result) {
                        currentSearchResult = result;
                        onSearchResultChanged();
                    }
                    // public void onSuccess(ArrayList<UserVO> result) {
                    // int totalRows = searchResultGrid.getRowCount();
                    // int rowCount = 1;
                    // for (UserVO user : result) {
                    // searchResultGrid.setWidget(rowCount, 0, new
                    // Label(user.getDisplayName()));
                    // searchResultGrid.setWidget(rowCount, 1, new
                    // Label(user.getEnabled() ? "Yes" : "No"));
                    // searchResultGrid.setWidget(rowCount, 2, new
                    // Button("Edit"));
                    // rowCount++;
                    // }
                    // for (; rowCount < totalRows; rowCount++) {
                    // searchResultGrid.clearCell(rowCount, 0);
                    // searchResultGrid.clearCell(rowCount, 1);
                    // searchResultGrid.clearCell(rowCount, 2);
                    // }
                    // }
                });
    }

    private void onSearchResultChanged() {
        searchResultGrid.resizeRows(1);
        int rowCount = 1;
        for (UserVO user : currentSearchResult) {
            searchResultGrid.resizeRows(rowCount + 1);
            searchResultGrid.setWidget(rowCount, 0, new Label(user.getDisplayName()));
            searchResultGrid.setWidget(rowCount, 1, new Label(user.getEnabled() ? "Yes" : "No"));
            final String username = user.getUserName();
            Button editButton = new Button("Edit");
            editButton.addClickHandler(new ClickHandler() {

                public void onClick(ClickEvent event) {
                    onClickEditButton(username);
                }
            });
            searchResultGrid.setWidget(rowCount, 2, editButton);
            rowCount++;
        }
    }
}
